var map = null;
var pinId = 1;
var g_mapIsReady = false;
var g_markersById = new Array(); 
var g_markersByCookie = new Array(); 
var g_selectionMarker = null;
 
VEPushpin.OnMouseOverCallback = function(x, y, title, details)
{
return;
    try
    {
        window.external.OverlayClicked(title);
    }
    catch (e)
    {
        showError(e + "Mouse Over Error");
    }
}

// required export for VirtualEarth
function GetMap()
{
    try
    {
        map = new VEMap('myMap');
		map.onLoadMap = function(){ g_mapIsReady = true; };
        map.LoadMap();
		window.external.scriptLoadCompleted();
    }
    catch (e)
    {
        showError(e);
    }
}

function showObjectProperties(o)
{
    try
    {
        var property;
        var message = "";
        for (property in o)
            message = message + property + ", ";
        alert(message);
    }
    catch (e)
    {
        showError(e);
    }
}

function zoomIn()
{
    try
    {
        map.ZoomIn(); 
    }
    catch (e)
    {
        showError(e);
    }
}

function zoomOut()
{
    try
    {
        map.ZoomOut(); 
    }
    catch (e)
    {
        showError(e);
    }
}

function showDashboard()
{
    try
    {
        map.ShowDashboard(); 
    }
    catch (e)
    {
        showError(e);
    }
}

function hideDashboard()
{
    try
    {
        map.HideDashboard(); 
    }
    catch (e)
    {
        showError(e);
    }
}

// called by document
function resizeMap()
{
    try
    {
        // undocumented code, you need to do this
        // or the map thinks it is the old size and displays
        // wrong
        map.vemapcontrol.Resize(); 
    }
    catch (e)
    {
        showError(e);
    }
}

// GpxViewer interface

function mapIsReady()
{
	return g_mapIsReady;
}

function hideLabels()
{
}

function showLabels()
{
}

function zoomToMarkers()
{
    try
    {
        var minLat = 500;
        var minLon = 500;
        var maxLat = -500;
        var maxLon = -500;
        
        var pin;
        var i;
        for ( i = 1; i< pinId; i++)
        {
            var pin = g_markersById[i];
            if (pin == null)
                continue;
            var latLon = pin.LatLong;
            if (latLon == null)
                continue;
            
            if (latLon.Latitude < minLat)
                minLat = latLon.Latitude;
            if (latLon.Latitude > maxLat)
                maxLat = latLon.Latitude;
            if (latLon.Longitude < minLon)
                minLon = latLon.Longitude;
            if (latLon.Longitude > maxLon)
                maxLon = latLon.Longitude;
        }
        if (minLat == -500 ||minLon == -500 || maxLat == 500 || maxLon == 500)
            return;
            
        var minLatLon = new VELatLong(minLat,minLon);
        var maxLatLon = new VELatLong(maxLat,maxLon);
    
        map.SetMapView([minLatLon,maxLatLon]);
    }
    catch (e)
    {
        showError(e, "zoomToMarkers");
    }
}

// GpxViewer interface
function getCenterLatLon(latLon)
{
 	var veLatLon = map.GetCenter();
	latLon.DecimalLat = veLatLon.Latitude;
	latLon.DecimalLon = veLatLon.Longitude;
}

 function setCenterAndZoom(lat, lon, zoom) 
 { 
	map.SetCenterAndZoom(new VELatLong(lat, lon), zoom); 
 }
 
function centerOrPan(x,y)
{
    try
    {
    	map.SetCenter(new VELatLong(y,x));
    }
    catch (e)
    {
        showError(e, "centerOrPan");
    }
}

function removeSelection()
{
    try
    {
        if (g_selectionMarker == null)
            return;
            
        map.DeletePushpin(g_selectionMarker.PinId);
        g_selectionMarker = null;
    }
    catch (e)
    {
        showError(e, "removeSelection");
    }
}

function addPin(cookie, x, y, image, title, details, label)
{
    try
    {
        var latLong = new VELatLong(y,x);
        var pin = new VEPushpin(
            pinId,
            latLong,
            image,
            title,
            details,
            "PinStyle");

        pin.PinId = pinId;
        pin.LatLong = latLong;
        pin.Cookie = cookie;
        pin.Label = label;
        g_markersById[pinId] = pin;
        g_markersByCookie[cookie] = pin;
            
        map.AddPushpin(pin);
        pinId++;
        return pin;
    } 
    catch (e)
    {
        showError(e, "addPin");
    }
}

function showError (e, location)
{
    s = "An error occurred";
    if (location != null)
    {
        s += "at: " + location;
    }
    if (e.name != null)
    {
        s += "\nName: \n" + e.name+ "\n";
    }
    if (e.message != null)
    {
        s += "\nMessage: \n" + e.message + "\n";
    }
    if (e.source != null)
    {
        s += "\nSource: \n" + e.source + "\n";
    }
    alert (e.message + "\n" + e.source + "\n"  + e.name);
}


// GpxViewer interface
function addMarker(cookie, x, y, image, title, details)
{
    try
    {
        addPin(cookie,x,y,image,title,details,title);
    } 
    catch (e)
    {
        showError(e, "addMarker");
    }
}


// GpxViewer interface
function clearOverlays()
{
    try
    {
        removeSelection();
        g_markersById = new Array(); 
        g_markersByCookie = new Array(); 
        var x = 5;
        x = x/0;
        map.Clear();
    }
    catch (e)
    {
        showError(e, "clearOverlays");
    }
}

// GpxViewer interface
function removeMarker(cookie)
{
    try
    {
        removeSelection();
        var marker = g_markersByCookie[cookie];
        map.DeletePushpin(marker.PinId);
    }
    catch (e)
    {
        showError(e, "selectMarker");
    }
}

// GpxViewer interface
function selectMarker(cookie)
{
    try
    {
        removeSelection();
        var marker = g_markersByCookie[cookie];
        var latLong = marker.LatLong;
    
        g_selectionMarker = addPin(
            "_"+marker.Cookie,
            latLong.Longitude, 
            latLong.Latitude,
            "halo.gif",
            marker.Title,
            marker.Details,
            "");
    }
    catch (e)
    {
        showError(e, "selectMarker");
    }
}

// from http://www.viavirtualearth.com/MyVirtualEarth/v3/dynamicpushpin.htm
// http://www.viavirtualearth.com/VVE/Blog/1/133.ashx

function onPinLoad(pin)
{
    pin.style.zIndex = 102;
    pin.parentNode.style.zIndex = 102;
}

VEPushpin.prototype.GetContent= function()
{
    var pinId=this.ID+"_"+this.m_vemap.GUID;
    var content="<img onload='onPinLoad(this)' class='"+this.IconStyle+"' src='"+this.Iconurl+"' id='"+pinId+"' ";
    
    var isTitleValid=this.Title!=null&&this.Title!="undefined"&&this.Title.length>0;
    
    var isDetailsValid=this.Details!=null&&this.Details!="undefined"&&this.Details.length>0;
    
    if(isTitleValid||isDetailsValid)
    {
        content+=" onmouseout='VEPushpin.Hide();' onmousedown='VEPushpin.Hide(true);' onmouseover='VEPushpin.Show(\""
          +this.m_vemap.GUID
          +"\",\""
          +this.ID
          +"\","
          +this.LatLong.Latitude
          +","+this.LatLong.Longitude;
    
        if(isTitleValid)content+=", \""+escape(this.Title)+"\"";
    	  else content+=",\"\"";
    
        if(isDetailsValid)content+=", \""+escape(this.Details)+"\"";
        else content+=",\"\"";
      
        content+=",\""+this.TitleStyle+"\"";
        content+=",\""+this.DetailsStyle+"\"";
        content+=");' ";
    
    }
    //this adds the click event 	
    content += " onclick='PinClick(" + this.ID + ")'";
    content += " oncontextmenu='PinRClick(" + this.ID + ")'";
    
    content+=" >";
    
    content += this.Label;

    
    
    return content;
}

function PinRClick(id)
{
    try
    {
        window.external.OverlayRightClicked(g_markersById[id].Cookie);
    }
    catch (e)
    {
        showError(e);
    }
}
    
function PinClick(id)
{
    try
    {
		window.external.OverlayClicked(g_markersById[id].Title);
    }
    catch (e)
    {
        showError(e + "Pin Click Error");
    }
}
